package com.hongliang.netty01.future;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author Hongliang Zhu
 * @create 2021-09-25 22:43
 */
@Slf4j
public class NettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoop eventLoop = new NioEventLoopGroup().next();

        // 主动创建 promise  结果的容器
        DefaultPromise promise = new DefaultPromise(eventLoop);

        new Thread(()->{
            log.debug("开始计算...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            promise.setSuccess(90);
        }).start();

        // main线程
        log.debug("等待结果。。");
        log.debug("结果是： {}", promise.get());
    }
}
